﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GenericDbObject_Ctor_Should.cs" company="MyCoreLib">
//   Copyright (C) 2014 Alexey Evlampiev. All rights reserved.
// </copyright>
// <summary>
//   Defines the GenericDbObject_Ctor_Should type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace MyCoreLib.Automation.UnitTests.Data.SqlClient
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Automation.Data.SqlClient;

    [TestClass]
    public class GenericDbObject_Ctor_Should
    {
        [TestMethod]
        public void SucceedForValidFullName()
        {
            var validFullNames = new String[]
            {
                "data.MyProcedure",
                "[data].[MyProcedure]",
                "[data].MyProcedure",
                "data.[MyProcedure]",
                " [  data  ] . [ MyProcedure ] ",
                "  data  .  MyProcedure  "
            };
            foreach (var fullName in validFullNames)
            {
                var dbObject = new GenericDbObject(fullName);
                Assert.AreEqual("data", dbObject.Schema, "Failed to determine the db objects schema. Test sample: {0}", fullName);
                Assert.AreEqual("MyProcedure", dbObject.Name, "Failed to determine the db objects name. Test sample: {0}", fullName);
            }
        }

        [TestMethod]
        public void SucceedForSchemalessName()
        {
            var validFullNames = new String[]
            {
                "MyProcedure",
                "[MyProcedure]",
                " [ MyProcedure ]",
                "  MyProcedure  "
            };
            foreach (var fullName in validFullNames)
            {
                var dbObject = new GenericDbObject(fullName);
                Assert.AreEqual("dbo", dbObject.Schema, "Failed to determine the db objects default schema (dbo)");
                Assert.AreEqual("MyProcedure", dbObject.Name, "Failed to determine the db objects name");
            }
        }


        [TestMethod()]
        [ExpectedException(typeof(FormatException))]
        public void FailForInvalidName()
        {
            var dbObject = new GenericDbObject("[dbo . [MyProcedure");
        }

    }
}
